<template>
  <BaseDialog
    v-model="dialogVisible"
    :title="g_t('fleet.costManage')"
    width="850px"
    :before-close="handleClose"
    body-class="dialog-table-class"
  >
    <div class="content-container">
      <el-row class="dialog-table-filter">
        <!-- <div class="row-left">
          <TextInput
            v-model="listParams.expenseItem"
            class="item"
            clearable
            :placeholder="$t('fleet.expenseItem')"
          />
          <el-button type="primary" @click="handleQuery">{{
            $t('public.search')
          }}</el-button>
          <el-button plain @click="handleReset">{{
            $t('public.reset')
          }}</el-button>
        </div> -->
        <div class="row-right">
          <el-button type="primary" @click="handleCreate"
            >+ {{ $t('public.create') }}</el-button
          >
        </div>
      </el-row>
      <div class="base-table-container">
        <BaseTable
          ref="refContentTable"
          v-loading="loading"
          :data="tableData"
          row-key="id"
        >
          <TableColumn
            prop="name"
            :label="$t('fleet.expenseItem')"
            :column-width="columnWidthObj.name"
          />
          <TableColumn
            prop="amount"
            :label="$t('hotel.amount')"
            :column-width="columnWidthObj.amount"
          />
          <TableColumn
            prop="createTime"
            :label="$t('fleet.expenseDate')"
            :column-width="columnWidthObj.createTime"
          />
          <TableColumn
            prop="note"
            :label="$t('public.remark')"
            :column-width="columnWidthObj.note"
          />
          <TableColumn
            prop="action"
            fixed="right"
            :label="$t('public.action')"
            :column-width="columnWidthObj.action"
          >
            <template #default="{ row }">
              <el-button text @click="handleDelete(row)">
                {{ $t('public.delete') }}
              </el-button>
              <el-button text @click="handleEditDialog(row)">
                {{ $t('public.edit') }}
              </el-button>
            </template>
          </TableColumn>
        </BaseTable>
        <BasePagination
          v-model:page-size="pageSize"
          v-model:current-page="pageNum"
          :total="total"
          @change="getTableList()"
        />
      </div>
      <!-- 新建 -->
      <AddCost
        v-if="addOrEditVisible"
        v-model="addOrEditVisible"
        :order-id="props.orderId"
        :row="tableRow"
        :type="addOrEditType"
        @update="handleUpdate"
      />
    </div>
    <template #footer>
      <div class="detail-dialog-footer">
        <el-button plain @click="handleClose">{{
          $t('public.close')
        }}</el-button>
      </div>
    </template>
  </BaseDialog>
</template>

<script setup>
import AddCost from './AddCost.vue'

const props = defineProps({
  type: {
    type: String,
    default: 'add' // add | view
  },
  orderId: {
    type: String,
    default: ''
  }
})

const dialogVisible = defineModel({
  type: Boolean,
  default: false
})
const emit = defineEmits(['update'])

// const listParams = ref({
//   orderId: ''
// })
const refContentTable = ref(null)
const loading = ref(false)
const tableData = ref([])
const columnWidthObj = ref({})
const total = ref(0)
const pageSize = ref(10)
const pageNum = ref(1)
const addOrEditVisible = ref(false)
const addOrEditType = ref('detail')
const tableRow = ref('')

const getTableList = () => {
  loading.value = true
  tableData.value = []
  const params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    orderId: props.orderId
  }
  g_api.fleet
    .apiExpenseList(params)
    .then(({ code, data }) => {
      if (code === '200' && data) {
        tableData.value = data.items
        total.value = data.total
        useTableColumnWidth({
          refTable: refContentTable.value
        }).getTableColumnWidth(data => {
          columnWidthObj.value = data
        })
      }
    })
    .finally(() => {
      loading.value = false
    })
}

onMounted(() => {
  getTableList()
})

const handleClose = () => {
  dialogVisible.value = false
}

const handleQuery = () => {
  pageNum.value = 1
  getTableList()
}

const handleCreate = () => {
  addOrEditVisible.value = true
  addOrEditType.value = 'add'
}

const handleDelete = row => {
  g_utils
    .deleteConfirm()
    .then(() => {
      loading.value = true
      g_api.fleet
        .apiExpenseDelete({
          orderId: props.orderId,
          expenseList: [
            {
              id: row.id,
              amount: row.amount
            }
          ]
        })
        .then(res => {
          if (res.code === '200') {
            ElMessage.success(res.message)
            handleUpdate()
          }
        })
        .finally(() => {
          loading.value = false
        })
    })
    .catch(() => {})
}

const handleEditDialog = row => {
  addOrEditVisible.value = true
  addOrEditType.value = 'edit'
  tableRow.value = row
}

const handleUpdate = () => {
  handleQuery()
  emit('update')
}
</script>

<style lang="scss">
.dialog-table-class {
  display: flex;
  .content-container {
    height: initial;
    .base-table-container {
      padding: 0;
    }
  }
}
</style>

<style lang="scss" scoped></style>
